//
//  LKDiscountTicketYesViewController.m
//  YLHelper
//
//  Created by she1323 on 2018/5/4.
//  Copyright © 2018年 she1323. All rights reserved.
//

#import "LKDiscountTicketYesViewController.h"
#import "LKWorkOrderListDetailOnlyImageCell.h"
#import "LKOrderEditBottomView.h"
#import "YLInvestmentTableViewCell.h"
#import "YLInvestmentCertificateModel.h"
@interface LKDiscountTicketYesViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)LKBaseTableview *tableView;
@property(nonatomic,strong)LKOrderEditBottomView *bottomView;
@property(nonatomic,strong)NSMutableArray *dataArray;
@property(nonatomic,strong)NSMutableArray *selectsIndexArray;
@end

@implementation LKDiscountTicketYesViewController
-(RACSubject *)pushSubject{
    if (!_pushSubject) {
        _pushSubject = [RACSubject subject];
    }
    return _pushSubject;
}
-(RACSubject *)popSubject{
    if (!_popSubject) {
        _popSubject = [RACSubject subject];
    }
    return _popSubject;
}
-(LKBaseTableview *)tableView{
    if (!_tableView) {
        WS(weakSelf)
        _bottomView = [[[NSBundle mainBundle] loadNibNamed:@"LKOrderEditBottomView" owner:self options:nil] lastObject];
        [self.view addSubview:_bottomView];
        [_bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self.view);
            make.height.mas_equalTo(69);
            
        }];
        [[_bottomView.forwardBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            [weakSelf.popSubject sendNext:@""];
        }];
        [[_bottomView.nextBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            NSMutableArray *voursArra = [[NSMutableArray alloc] init];
            for (NSIndexPath *selectIndex in self.selectsIndexArray) {
                YLInvestmentCertificateModel *model = weakSelf.dataArray[selectIndex.section];
                NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:model.customerVoucherId,@"CustomerVoucherId",model.voucherName,@"VoucherName",@(YES),@"IsSuperposition", nil];
                [voursArra addObject:param];
            }
            weakSelf.addOrderModel.OrderVoucher = voursArra.mutableCopy;
            [weakSelf.pushSubject sendNext:weakSelf.addOrderModel];
        }];
        _tableView = [LKBaseTableview setTableviewWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - (NAVIGATION_BAR_HEIGHT + 45 + 70)) style:UITableViewStyleGrouped registercellClasss:@[@"LKWorkOrderListDetailOnlyImageCell",@"YLInvestmentTableViewCell"]];
        [self.view addSubview:_tableView];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.emptyDataSetSource = self;
        _tableView.emptyDataSetDelegate = self;
    }
    return _tableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    if (self.isOrderUpdate) {
        self.tableView.frame = CGRectMake(0, 0, WIDTH, HEIGHT - (NAVIGATION_BAR_HEIGHT + 45 + 10));
        [self.bottomView removeFromSuperview];
        self.tableView.estimatedRowHeight = 200;
        [self getDataArrayList];
    }else{
        self.tableView.estimatedRowHeight = 200;
        [self getDataArrayList];
    }
    
    // Do any additional setup after loading the view.
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    YLInvestmentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YLInvestmentTableViewCell"];
    for (NSIndexPath *index in self.selectsIndexArray) {
        if (indexPath == index) {
            cell.backgroundColor = blueDefault;
        }else{
            cell.backgroundColor = [UIColor whiteColor];
        }
    }
    cell.model = self.dataArray[indexPath.section];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 145;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.0000000000001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 13;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [[UIView alloc] init];
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] init];
//    view.backgroundColor = cellHeaderBgColor;
        view.backgroundColor = [UIColor whiteColor];
    return view;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([self.selectsIndexArray containsObject:indexPath]) {
        [self.selectsIndexArray removeObject:indexPath];
    }else{
       [self.selectsIndexArray addObject:indexPath];
    }
    [self.tableView reloadData];
}
-(void)getDataArrayList{
    NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:self.addOrderModel.IdentityId,@"IdentityId",self.addOrderModel.ProductCode,@"ProductCode",self.addOrderModel.CalulationDate,@"CalulationDate",self.addOrderModel.InvestmentAmount,@"InvestmentAmount",@(YES),@"IsIsSuperposition", nil];
    [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:getVoucherListUrl header:[LKUserInforTool header] parameters:param progress:^(NSProgress * _Nullable progress) {
        
    } success:^(BOOL isSuccess, id  _Nullable responseObject) {
        NSArray *items = responseObject[@"result"][@"items"];
        for (NSDictionary *dic in items) {
            YLInvestmentCertificateModel *model = [YLInvestmentCertificateModel modelWithDictionary:dic];
            [self.dataArray addObject:model];
        }
        [self.tableView reloadData];
    } failure:^(NSString * _Nullable errorMessage) {
        
    }];
}
-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}
-(NSMutableArray *)selectsIndexArray{
    if (!_selectsIndexArray) {
        _selectsIndexArray = [[NSMutableArray alloc] init];
    }
    return _selectsIndexArray;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
